<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\BaseController;
use think\Request;
use think\facade\View;

class Api extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $config = [
            'title' => sysconfig('site', 'site_name'),
            'sitename' => 'sitename',
            'apiurl' => sysconfig('site', 'site_domain'),
        ];
        View::assign('config', $config);

        $language = 'zh-cn';

        $langDir = __DIR__ . DS . '..' . DS;
        $language = $language ? $language : 'zh-cn';
        $langFile = $langDir . 'lang' . DS . $language . '.php';
        if (!is_file($langFile)) {
            throw new \Exception('language file not found');
        }

        $lang = include $langFile;
       
        View::assign('lang', $lang);

        $headersList = [
            [
                'name' => 'language',
                'description' => '多语言',
                'sample' => 'zh-cn',
                'type' => '',
                'required' => 0,
            ]
        ];
        $groups = \app\admin\model\system\SystemApiGroup::where('status', 1)->select();
        $docsList = [];
        foreach ($groups as $group) {
            $apilist = \app\common\model\Apilist::where('status', 1)
            ->where('group', $group['id'])
            ->select();

            $docs = [];
            foreach ($apilist as $api) {
                switch ($api['method']) {
                    case 1:
                        $method = 'POST';
                    break;
                    case 2:
                        $method = 'POST';
                    break;
                    default:
                        $method = '不限';
                    break;
                }
                $Apifields = new \app\common\model\Apifields;
                $paramsList = $Apifields->where('hash', $api['hash'])
                ->select();
                $params = [];
                $returnParams = [];
                foreach ($paramsList as $item) {
                    if ($item['type'] == 1) {
                        $returnParams[] = [
                            'id' => $item['id'],
                            'name' => $item['fieldname'],
                            'sample' => $item['fieldname'],
                            'type' => $Apifields->datatype[$item['datatype']],
                            'required' => $item['ismust'],
                            'description' => $item['info'],
                        ];
                    } else {
                        $params[] = [
                            'id' => $item['id'],
                            'name' => $item['fieldname'],
                            'sample' => $item['fieldname'],
                            'type' => $Apifields->datatype[$item['datatype']],
                            'required' => $item['ismust'],
                            'description' => $item['info'],
                        ];
                    }
                    
                }
                $docs[] = [
                    'id' => $api['id'],
                    'title' => $api['info'],
                    'needRight' => 0,
                    'needLogin' => $api['needlogin'],
                    'methodLabel' => 'label-danger',
                    'method' => $method,
                    'route' => '/api/v1/'.$api['hash'],
                    'apiname' => $api['apiname'],
                    'summary' => $api['readme'],
                    'headersList' => $headersList,
                    'paramsList' => $params,
                    'returnParamsList' => $returnParams,
                    'return' => $api['returnstr'],
                ];
            }
            $docsList[$group['name']] = $docs;
        }

        
        View::assign('docsList', $docsList);
        return View::fetch();
    }
}
